<?
/*
 * accountCreate.php serves as the central point of interaction 
 * for the user with Swindells for event registration, library 
 * item checkout and account creation.
 */

$rightCol = 1;
$step = 1;
$errorDivDisplay = 'none';
include('includes/appInit.php');
$events  = new Events();
$library = new Library();
$disData = new getData();
$mail    = new Emails();
$access  = new accountAccess();

$itemType    = $_GET['itemType'];
$itemID      = $_GET['itemID'];
$saveAccount = $_GET['saveAccount'];

if (empty($itemType) && empty($itemID)) {
	/*
	 * Try looking in the $_POST collection
	 */
	$itemType = $_POST['itemType'];
	$itemID   = $_POST['itemID'];
}

if ($_GET['checkEmail'] == 1) {
	$email = $_POST['email'];
	/*
	 * Check for valid email address
	 */
	$emailValidator = new Zend_Validate_EmailAddress();
	if (empty($email) || !$emailValidator->isValid($email)) {
		$error = 1;
		$errorMessage = 'The email address '.$email.' appears to be invalid.';
		$errorDivDisplay = '';
	}
	if ($error != 1) {
		if (!$access->checkLogin($email)) {
			if ($_POST['action'] == 'newAccount') {
				$saveAccount = 1;
			} else {
				/*
				 * Move to step 2
				 */
				$step = 2;
			}	
		} else {
			if ( ($itemType == 'library') || ($itemType == 'event') ) {
				$step = 2;
			} else {
				/*
				 * Send user to account landing page.
				 */
				header('Location: accountHome.php?existingAccount=1');				
			}
		}
	}	
}

/*
 * What are we here to do?
 */
if (empty($itemType) && empty($itemID)) {
	/*
	 * Then we're just creating a new account.
	 * If we're already logged in, send back to account landing page.
	 */
	if ( ($_SESSION['logged'] == 1) && empty($_GET['edit']) ) {
		header('Location: accountHome.php');
	}
	if ($_GET['edit'] == 1) {
		$pageTitle = 'Update your account';
		$buttonLabel = 'Update';
		$action = 'updateAccount';
	} else {
		$pageTitle = 'Create your account';
		$buttonLabel = 'Register';
		$action = 'newAccount';
	}	
} elseif ($itemType == 'library') {
	/*
	 * Then we're checking out a library item.
	 */
	if (empty($itemID) || !is_numeric($itemID)) {
		header('Location: libraryView.php');
	}
	
	if ($_SESSION['logged'] == 1) {
		$email = $_SESSION['email'];
		if ($library->determineCheckoutStatus($_SESSION['memberID'],$itemID) == 2) {
			$error = 1;
			$errorMessage = 'You currently have this item checked out. ';
			/*
			 * When is this item due to be returned 
			 */
			$daysOverdue = $library->determineReturnDate($_SESSION['memberID'],$itemID);
			
			if ($daysOverdue<0) {
				$dateToReturn = dateFromDays(date('Y-m-d'),abs($daysOverdue));
				$dateToReturn = new Zend_Date($dateToReturn, Zend_Date::ISO_8601, 'en_US');
				$newDateToReturn = $dateToReturn->toString('MMM d');
			
				$errorMessage .= 'This item needs to be returned in '.abs($daysOverdue).' days on '.$newDateToReturn.'.';
			} elseif ($daysOverdue>0) {
				$dateToReturn = dateFromDays(date('Y-m-d'),(0-$daysOverdue));
				$dateToReturn = new Zend_Date($dateToReturn, Zend_Date::ISO_8601, 'en_US');
				$newDateToReturn = $dateToReturn->toString('MMM d');
			
				$errorMessage .= 'This item is overdue and was to be returned '.$daysOverdue.' days ago on '.$newDateToReturn.'.';
			} elseif ($daysOverdue == 0) {
				$errorMessage .= 'This item needs to be returned today.';
			}
			$step = 0;
		} else {
			$step = 2;
		}	
	}
	$pageTitle = 'Checkout library item';
	$action = 'libraryCheckout';
	$buttonLabel = 'Checkout';
	$libraryItemArray = $library->getLibraryItem($itemID,'array');
	$orgFlag = 'Swindells';
	
} elseif ($itemType == 'event') {
	/*
	 * Then we're registering for an event.
	 */
	if (empty($itemID) || !is_numeric($itemID)) {
		header('Location: events.php');
	}
		
	if ($_SESSION['logged'] == 1) {
		$email = $_SESSION['email'];
		if ($events->checkRegistration($_SESSION['memberID'],$itemID)) {
			$error = 1;
			$errorMessage = 'You have already registered for this event.';
			$step = 0;
		} else {
			$step = 2;
		}	
	}
	$buttonLabel = 'Register';
	$pageTitle = 'Register for an event';
	$action = 'eventRegistration';
	$eventArray = $events->getEventDetails($itemID,'array');
	$orgFlag = $eventArray['OrgFlag'];
}

if ( ($_GET['saveAccount'] == 1) || ($saveAccount == 1) ) {
	$saveInfoArray = $access->saveAccountInformation();
}

if (!empty($_SESSION['memberID'])) {
	$memberID = $_SESSION['memberID'];
	$memberData = new accountInfo();
	$memberArray = $memberData->getMemberData($memberID);
	$nameFirst = $memberArray['FirstName'];
	$nameLast  = $memberArray['LastName'];
	$address   = DBToField($memberArray['Address']);
	$city      = $memberArray['City'];
	$state     = $memberArray['State'];
	$zip       = $memberArray['Zip'];
	$email     = $memberArray['Email1'];
	$phone     = $memberArray['Phone1'];
	$memberType= $memberArray['MemberType'];
	$childAge  = $memberArray['ChildAge'];
	$orgName   = $memberArray['OrganizationName'];
	$orgTitle  = $memberArray['OrganizationTitle'];	
}	

if ($error == 1) {
	$errorDivDisplay = '';
}

include('includes/header.php');
include('includes/registrationFormJavaScript.php');
?>
	<div id="subPage">
		<div class="leftCol">
		
			<div id="subPageContent" class="flex">
				<h1><?=$translate->_($pageTitle); ?></h1>
				<?	if ($action == 'newAccount') { ?>
				<p>Creating an personal account on our webiste gives you many benefits including
				quick and simple <a href="events.php">event registration</a>, fast book checkout from our <a href="library.php">library</a>, 
				saved search results and much more.</p><br />
				
				<p>If you have ever shared your email address with the Child Disability Connection, chances
				are good that you already have an account with us. Enter your email address below to check.</p>
				<? } elseif ($action == 'eventRegistration') { ?>
					<div style="float: left; width: 50%;"><h2><?=$eventArray['EventName']; ?></h2></div>
					<div style="float: right; width: 50%; text-align: right;"><h3 style="text-align: right;"><?=$eventArray['DateString']; ?></h3></div>
					<div style="clear: both; height: 10px;"></div>
				<? } elseif ($action == 'libraryCheckout') { ?>
					<p style="margin-top: -10px; margin-bottom: 15px;" class="bulletOrangeStar"><i><b>NOTE:</b> Only 1 book may be checked out at a time.</i></p>
					<div><h2><?=$libraryItemArray['Title']; ?></h2>
						<p><i>by <?=$libraryItemArray['Authors']; ?></i></p>
					</div>
					
				<? } ?>					
				<br />
				
				<h3 style="margin-bottom: 10px;">Your Privacy</h3>
				<p>If you have any questions or concerns about your privacy on the Child Disability Connection webiste,
				please read our 
				<a href="privacyPolicy.php" target="_blank" onClick="window.open('privacyPolicy.php','privWin','width=500,height=600,status=0,toolbar=0,scrollbars=1,resizable=1'); return false;">
				Privacy Policy</a>.</p>
				
				
				<div style="height: 20px;"></div>
				<? include('includes/registrationForm.php'); ?>
			</div>
		</div>
		<div class="rightCol noPrint">
			<div class="rightColInner flex" style="height: 500px;">
				<? include('includes/rightColumn.php'); ?>
			</div>
		</div>
		<div style="clear: both;"></div>
	</div>	
	
	
<?
include('includes/footer.php');
?>